package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Strategy;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.keys.RedisKeys;
import cn.wolfcode.luowowo.cache.service.ITravelRedisService;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AJaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserService;
import cn.wolfcode.luowowo.search.service.ITravelTemplateSearchService;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的窝控制器
 */
@Controller
@RequestMapping("/mine")
public class MineController {

    @Reference
    private IUserService userService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;

    @Reference
    private ITravelRedisService travelRedisService;

    @Reference
    private ITravelCommentService commentService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @RequestMapping("/home")//我的窝
    public String home(Model model,@UserParam UserInfo userInfo,@ModelAttribute("qo")TravelQuery qo){
        model.addAttribute("userInfo",userInfo);
        PageInfo pageInfo = travelService.query(qo);
        List<Travel> list = pageInfo.getList();
        if(list.size() >5){
            list.subList(0,5);
        }
        List<Travel> ts =new ArrayList<>();
        for (Travel travel : list) {
            Destination dest = destinationService.getById(travel.getDest().getId());
            TravelStatisVO vo = travelRedisService.getStatisVo(travel.getId());
            travel.setDest(dest);//将当前目的地(含上一级目的地id)设置到游记中
            travel.setViewnum(vo.getViewnum());
            travel.setReplynum(vo.getReplynum());
            travel.setFavornum(vo.getFavornum());
            travel.setThumbsupnum(vo.getThumbsupnum());
            travel.setSharenum(vo.getSharenum());
            ts.add(travel);
        }
        model.addAttribute("total", travelService.list().size());

        List<TravelComment> tcs = commentService.getCommentByUserId(userInfo.getId());
        model.addAttribute("tcs", tcs);


        model.addAttribute("ts", ts);
        return "mine/homepage";
    }


    @RequestMapping("/mytravelnotes")
    public String mytravelnotes(Model model, @ModelAttribute("qo")TravelQuery qo,@UserParam UserInfo userInfo){
        qo.setUserId (userInfo.getId ());
        PageInfo pageInfo = travelService.query(qo);
        List<Travel> newList= new ArrayList<>();
        List<Travel> list = pageInfo.getList();
//        List<TravelStatisVO> voList = new ArrayList<>();
        model.addAttribute("userInfo",userInfo);//共享当前用户
        TravelStatisVO vo = null;//将回复数,阅读数设置到vo对象中
        int totalReplynum = 0;//总回复数
        int totalViewnum = 0;//总阅读数
        for (Travel travel : list) {
            Destination dest = destinationService.getById(travel.getDest().getId());
            travel.setDest(dest);
            newList.add(travel);

            //回复数
            vo = travelRedisService.getStatisVo(travel.getId());
            totalReplynum += vo.getReplynum();
            //阅读数
            totalViewnum += vo.getViewnum();
            // voList.add(vo);
        }
        vo.setReplynum (totalReplynum);
        vo.setViewnum(totalViewnum);
        // model.addAttribute("voList", voList);
        model.addAttribute("vo", vo);
        pageInfo.setList(newList);
        model.addAttribute("pageInfo", pageInfo);

        return "mine/mytravelnotes";
    }

    @RequestMapping("/travelThumbup")
    @ResponseBody
    public Object travelThumbup(Long tid,@UserParam UserInfo userInfo){
        boolean ret = travelRedisService.travelThumbup(tid, userInfo.getId());

        return new AJaxResult(ret, "").addData(travelRedisService.getStatisVo(tid));
    }
    @RequestMapping("/review")
    public String review(Model model,@UserParam UserInfo userInfo){

        List<TravelComment> tcs = commentService.getCommentByUserId(userInfo.getId());
        List<Travel> ts = new ArrayList<>();


        TravelStatisVO vo = null;
        for (TravelComment tc : tcs) {
            Travel travel = travelService.get(tc.getTravelId());
            vo = travelRedisService.getStatisVo(travel.getId());
            travel.setViewnum(vo.getViewnum());
            travel.setFavornum(vo.getFavornum());
            travel.setThumbsupnum(vo.getThumbsupnum());
            travel.setSharenum(vo.getSharenum());
            ts.add(travel);



        }
        //点评数,点赞数---vo对象
        vo.setReplynum(tcs.size());

        model.addAttribute("vo", vo);


        model.addAttribute("ts", ts);
        model.addAttribute("tcs", tcs);
        return "mine/review";
    }

    //个人中心的收藏的统计
    @RequestMapping("/travelcollection")
    public String travelcollection(Model model, @UserParam UserInfo userInfo){
        List<Travel> travelList = travelService.getTravelsByUserId(userInfo.getId ());
        List<StrategyDetail> strategyDetailList = strategyDetailService.getStrategyByUserId(userInfo.getId ());
        model.addAttribute ("travelList", travelList);
        model.addAttribute ("strategyDetailList", strategyDetailList);






        return "mine/travelcollection";
    }

    @RequestMapping("/setting")
    public String setting(Model model, @UserParam UserInfo userInfo){

        model.addAttribute("userInfo",userService.get(userInfo.getId()));

        return "mine/setting";
    }

    @RequestMapping("/update")
    @ResponseBody
    public AJaxResult update(UserInfo userInfo){

        AJaxResult json = new AJaxResult();
        try {
            userService.update(userInfo);

       } catch (Exception e) {
             e.printStackTrace();
           json.mark(e.getMessage());
       }
       return json;
    }

    //图片上传
    @Value("${file.path}")
    private String filePath;

    @RequestMapping("/uploadImg")
    public  Object uploadImg(MultipartFile file){
        String filename = UploadUtil.upload(file,filePath);
        return filename;
    }

    @RequestMapping("/updateHeadImg")
    @ResponseBody
    public AJaxResult updateHeadImg(@UserParam UserInfo userInfo, String headImgUrl){
        //当用户没有登录的时候,提示登录
        if (userInfo == null) {
            return new AJaxResult(false, "修改头像需要登录哦");
        }

        userService.updateHeadImg(headImgUrl,userInfo.getId());

        return AJaxResult.SUCCESS();
    }

    //修改密码
    @RequestMapping("/updatePassword")//修改密码
    @ResponseBody
    public Object updatePassword(String rpassword,@UserParam UserInfo userInfo){
        //当用户没有登录的时候,提示登录
        if (userInfo == null) {
            return new AJaxResult(false, "修改密码需要登录哦");
        }
        userService.updatePassword(rpassword,userInfo.getId());
        return AJaxResult.SUCCESS();
    }

    //修改手机号
    @RequestMapping("/changePhone")//修改电话号码
    @ResponseBody
    public Object changePhone(String newMobile,@UserParam UserInfo userInfo,String newVerifyCode){
        //当用户没有登录的时候,提示登录
        if (userInfo == null) {
            return new AJaxResult(false, "修改手机号码需要登录哦");
        }

        if (!(userService.changePhone(newMobile,userInfo.getPhone(),newVerifyCode))){
            return new AJaxResult(false,"验证码错误");
        }
        return AJaxResult.SUCCESS();
    }

}
